﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace UPNP.IO
{
    internal sealed class TcpIncomingRequest : TcpIncomingBase
    {
        public enum TcpRequestProtocol
        {
            SsdpDiscovery,
            Post,
            ManPost,
            Subscribe,
            Unsubscribe,
            Notify
        }

        public TcpRequestProtocol Protocol
        {
            get; 
            private set;
        }

        private TcpRequestProtocol ParseStatus()
        {
            if (this.StatusHeader.StartsWith("SUBSCRIBE "))
                return TcpRequestProtocol.Subscribe;

            if (this.StatusHeader.StartsWith("UNSUBSCRIBE "))
                return TcpRequestProtocol.Unsubscribe;

            if (this.StatusHeader.StartsWith("NOTIFY "))
                return TcpRequestProtocol.Notify;

            if (this.StatusHeader.StartsWith("POST "))
                return TcpRequestProtocol.Post;

            if (this.StatusHeader.StartsWith("M-POST "))
                return TcpRequestProtocol.ManPost;

            if (this.StatusHeader.StartsWith("M-SEARCH "))
                return TcpRequestProtocol.SsdpDiscovery;

            throw new Exception("Unknown response protocol: " + this.StatusHeader);
        }

        public TcpIncomingRequest(EndPoint sender, byte[] response)
            : base(sender, response)
        {
            Contract.Requires(sender != null);
            Contract.Requires(response != null);
        }

        public TcpIncomingRequest(EndPoint sender, string response)
            : base(sender, response)
        {
            Contract.Requires(sender != null);
            Contract.Requires(!string.IsNullOrEmpty(response));
        }
    }
}
